<template>
  <div class="chart-container" ref="chartRef"></div>
</template>

<script setup>
import { ref, onMounted, nextTick } from 'vue'
import * as echarts from 'echarts'

const chartRef = ref(null)
let chartInstance = null

onMounted(() => {
  nextTick(() => {
    initChart()
    window.addEventListener('resize', handleResize)
  })
})

const initChart = () => {
  chartInstance = echarts.init(chartRef.value)

  const option = {
    title: {
      text: '今日医院牙周病患者统计',
      left: 'center'
    },
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross',
        crossStyle: {
          color: '#999'
        }
      }
    },
    legend: {
      data: ['患者数量'],
      left: 'left'
    },
    xAxis: {
      type: 'category',
      data: [
        '8:00',
        '9:00',
        '10:00',
        '11:00',
        '13:00',
        '14:00',
        '15:00',
        '16:00'
      ],
      axisPointer: {
        type: 'shadow'
      }
    },
    yAxis: {
      type: 'value'
    },
    series: [
      {
        name: '患者数量',
        type: 'bar',
        data: [5, 8, 12, 7, 9, 11, 6, 4]
      }
    ]
  }

  chartInstance.setOption(option)
}

const handleResize = () => {
  chartInstance && chartInstance.resize()
}

// const onUnmounted = () => {
//   window.removeEventListener('resize', handleResize);
//   chartInstance && chartInstance.dispose();
// };
</script>

<style scoped>
.chart-container {
  width: 100%;
  height: 300px;
}
</style>
